package com.noocodecommit.wicket.jquery.ajax;

import org.apache.wicket.ajax.AjaxEventBehavior;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.model.Model;

import com.noocodecommit.wicket.jquery.TemplatePage;
import com.noocodecommit.wicket.stuff.jquery.JQCore;
import com.noocodecommit.wicket.stuff.jquery.behavior.AbstractDefaultJQBehavior;
import com.noocodecommit.wicket.stuff.jquery.core.JQCoreUtils;
import com.noocodecommit.wicket.stuff.jquery.core.JQFunction;
import com.noocodecommit.wicket.stuff.jquery.plugin.chili.JQChili;
import com.noocodecommit.wicket.stuff.jquery.plugin.ui.tabs.JQTabs;

public class AjaxPage extends TemplatePage
{

	private final JQTabs tabContainer;

	public AjaxPage()
	{
		super();
		this.setupChili();

		tabContainer = new JQTabs("tab-container");
		tabContainer.setOutputMarkupId(true);
		add(tabContainer);

		// **************************************
		// * LOAD + JAVASCRIPT CALLBACK
		// **************************************
		final Label output1 = new Label("output1");
		output1.setOutputMarkupId(true);
		tabContainer.add(output1);

		final Label output2 = new Label("output2");
		output2.setOutputMarkupId(true);
		tabContainer.add(output2);

		final Button button1 = new Button("button1");
		tabContainer.add(button1);

		// -- ONCLICK
		button1.add(new AjaxEventBehavior("onclick")
		{
			protected void onEvent(AjaxRequestTarget target)
			{
				// -- JQuery Ajax call
				JQFunction callback = new JQFunction(JQCoreUtils.$(output1).html("This text is written by a javascript method called as &#39;load&#39; callback function.").build());
				target.appendJavascript(JQCoreUtils.$(output2).load("text/ajax1.txt", null, callback).build());
			}
		});

		// **************************************
		// * LOAD + WICKET BEHAVIOR CALLBACK
		// **************************************
		final Label output3 = new Label("output3");
		output3.setOutputMarkupId(true);
		tabContainer.add(output3);

		final Label output4 = new Label("output4");
		output4.setOutputMarkupId(true);
		tabContainer.add(output4);

		final Button button2 = new Button("button2");
		tabContainer.add(button2);

		// -- ONCLICK
		button2.add(new AjaxEventBehavior("onclick")
		{
			protected void onEvent(AjaxRequestTarget target)
			{
				// -- Callback behavior
				AbstractDefaultJQBehavior callback = new AbstractDefaultJQBehavior(false)
				{
					@Override
					protected void respond(AjaxRequestTarget target)
					{
						output3.setModel(new Model("This text is written by a behavior called as 'load' callback function."));
						target.addComponent(output3);
					}
				};
				add(callback);

				// -- JQuery Ajax call
				target.appendJavascript(JQCoreUtils.$(output4).load("text/ajax1.txt", null, callback).build());
			}
		});

		// **************************************************************
		// * LOAD + WICKET BEHAVIOR CALLBACK + BEHAVIOR SUCCESS SCRIPT
		// **************************************************************
		final Label output5 = new Label("output5");
		output5.setOutputMarkupId(true);
		tabContainer.add(output5);

		final Label output6 = new Label("output6");
		output6.setOutputMarkupId(true);
		tabContainer.add(output6);

		final Button button3 = new Button("button3");
		tabContainer.add(button3);

		// -- ONCLICK
		button3.add(new AjaxEventBehavior("onclick")
		{
			protected void onEvent(AjaxRequestTarget target)
			{
				// -- Callback behavior
				AbstractDefaultJQBehavior callback = new AbstractDefaultJQBehavior(false)
				{
					@Override
					protected void respond(AjaxRequestTarget target)
					{
						// do server stuff
					}

					@Override
					protected CharSequence getSuccessScript()
					{
						return JQCoreUtils.$(output5).html("This text is written by the &#39;success&#39; script of a Wicket behavior called as &#39;load&#39; callback function.").build();
					}

				};
				add(callback);

				// -- JQuery Ajax call
				target.appendJavascript(JQCoreUtils.$(output6).load("text/ajax1.txt", null, callback).build());
			}
		});
		// ********************************
		JQChili chili1 = new JQChili("chili1");
		add(chili1);
		this.bindChiliCode(chili1, this.getClass(), "html");

		JQChili chili2 = new JQChili("chili2");
		add(chili2);
		this.bindChiliCode(chili2, this.getClass(), "java");

		JQCore.get().bindOnDocumentReadyFunction();
	}
}
